package com.example.slogantv;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;


public class SplashAty extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private ImageView imgFocus, imgFocus1, imgSet, imgMuyin;
    private RelativeLayout root;
    private static Handler handler = new Handler();

    private static Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 100);

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        imgFocus = findViewById(R.id.imgFocus);
        imgFocus1 = findViewById(R.id.imgFocus1);
        imgSet = findViewById(R.id.imgSet);
        imgMuyin = findViewById(R.id.imgMuyin);
        root = findViewById(R.id.root);
//        imgSet.animate().alpha(1.0f).setDuration(1200).start();
//        imgMuyin.animate().alpha(1.0f).setDuration(1200).start();
        imgSet.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                System.out.println("hasFocus=imgSet" + hasFocus);
                if (hasFocus) {
                    imgSet.animate().scaleX(1.07f).scaleY(1.05f).setDuration(100).start();
                    imgFocus.animate().scaleX(1.07f).scaleY(1.05f).setDuration(100).start();
                    imgFocus.setVisibility(View.VISIBLE);
                } else {
                    imgSet.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start();
                    imgFocus.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start();
                    imgFocus.setVisibility(View.INVISIBLE); } }});
        imgMuyin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                System.out.println("hasFocus+imgMuyin" + hasFocus);
                if (hasFocus) {
                    imgMuyin.animate().scaleX(1.07f).scaleY(1.05f).setDuration(100).start();
                    imgFocus1.animate().scaleX(1.07f).scaleY(1.05f).setDuration(100).start();
                    imgFocus1.setVisibility(View.VISIBLE);
                    if (countDownTimer != null) {//获取焦点，启动计时器
                        countDownTimer.start();
                    }
                } else {
                    imgMuyin.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start();
                    imgFocus1.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start();
                    imgFocus1.setVisibility(View.INVISIBLE);
                    if (countDownTimer != null) {//失去焦点，取消计时器
                        countDownTimer.cancel();
                    }
                }
            }
        });
        imgMuyin.setFocusable(true);
        imgMuyin.setFocusableInTouchMode(true);
//        imgMuyin.requestFocus();
        imgMuyin.setNextFocusRightId(R.id.imgSet);
        imgMuyin.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.out.println("delay click muyin ");
//                imgMuyin.performClick();
            }
        }, 3000);

        imgMuyin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SplashAty.this, PlayListActivity.class));
                finish();
            }
        });
        imgSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }
        });

        setCountDownTimer();

    }

    private void setCountDownTimer() {
        countDownTimer = new CountDownTimer(6000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                System.out.println("timer count down");
            }

            @Override
            public void onFinish() {
                System.out.println("timer count down finish");
//                startActivity(new Intent(MainActivity.this, ExoPlayActivity.class));
                startActivity(new Intent(SplashAty.this, PlayListActivity.class));
                finish();
                countDownTimer.cancel();
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        imgSet.animate().alpha(1.0f).setDuration(0).start();
        imgMuyin.animate().alpha(1.0f).setDuration(0).start();
        imgMuyin.requestFocus();
//        setCountDownTimer();
        if (countDownTimer != null) {
            countDownTimer.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
//        countDownTimer.cancel();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("destroy main");
        countDownTimer = null;
    }
}
